package com.wyp.controller.admin;

import com.wyp.pojo.Admins;
import com.wyp.service.impl.AdminsServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/admin")
public class ManagerManController {

    @Autowired
    private AdminsServiceImpl adminsService;

    @Autowired
    private BCryptPasswordEncoder encoder;

    @GetMapping("/manager_manage.html")
    public String tomanagerman(Model model) {
        List<Admins> adminsList = adminsService.qureyAllAdmins();
        model.addAttribute("alladmins", adminsList);
        return "admin/manager/manager_manage";
    }

    @GetMapping("/admin_delete.html/{id}")
    public String mandelete(@PathVariable("id") int id) {
        adminsService.deleteAdmin(id);
        return "redirect:/admin/manager_manage.html";
    }

    @GetMapping("/manager_update.html/{usernumber}")
    public String tomanupdate(
            @PathVariable("usernumber") String usernumber,
            Model model
    ) {
        Admins admins = adminsService.findByAdminaccount(usernumber);
        model.addAttribute("updateadmin", admins);
        return "admin/manager/manager_update";
    }

    @PostMapping("/manager_update.html/{id}")
    public String manupdate(
            @PathVariable("id") int id,
            @RequestParam("adminNumber") String adminNumber,
            @RequestParam("adminPassword") String adminPassword,
            Model model
    ) {
        Admins admins = adminsService.findByAdminaccount(adminNumber);
        admins.setAdminpassword(encoder.encode(adminPassword));
        boolean addadmin = adminsService.updateAdmin(admins);
        return "redirect:/admin/manager_manage.html";
    }

    @GetMapping("/manager_add.html")
    public String tomanadd() {
        return "admin/manager/manager_add";
    }

    @PostMapping("/manager_add.html")
    public String manadd(
            @RequestParam("adminNumber") String adminNumber,
            @RequestParam("adminPassword") String adminPassword,
            Model model
    ) {
        Admins admins = new Admins();
        admins.setAdminaccount(adminNumber);
        admins.setAdminpassword(encoder.encode(adminPassword));
        boolean addadmin = adminsService.addAdmin(admins);
        return "redirect:/admin/manager_manage.html";
    }

    @RequestMapping("/findAddadminnum")
    @ResponseBody
    public Map<String, Object> findAddadminnum(String username) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        boolean isExist;
        if (adminsService.findByAdminaccount(username) != null) {
            isExist = true;
        } else {
            isExist = false;
        }
        resultMap.put("isExist", isExist);
        return resultMap;
    }

}
